package com.egao.common.module.system.transfer;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.company.constants.CheckRecordConstants;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.entity.Field;
import com.egao.common.module.company.entity.Position;
import com.egao.common.module.company.entity.Recruit;
import com.egao.common.module.company.service.CompanyService;
import com.egao.common.module.company.service.FieldService;
import com.egao.common.module.company.service.PositionService;
import com.egao.common.module.company.service.RecruitService;
import com.egao.common.module.push.entity.PositionMatching;
import com.egao.common.module.push.service.PositionMatchingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 场地数据导入
 * @author: dl
 */
@RestController
@Slf4j
public class FieldDataController {

    @Value("${old.datasource.jyurl}")
    private String url;

    @Value("${old.datasource.username}")
    private String userName;

    @Value("${old.datasource.password}")
    private String password;

    @Autowired
    private FieldService fieldService;

    @Transactional(rollbackFor = Exception.class)
    public JsonResult transfer() {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        long beginTime = System.currentTimeMillis();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
            st = conn.createStatement();
            String sql = "SELECT " +
                    "ID," +
                    "type," +
                    "FieldName," +
                    "Number," +
                    "COMMENT," +
                    "DuraMax," +
                    "create_date," +
                    "del_flag," +
                    "create_by," +
                    "update_by," +
                    "update_date," +
                    "remarks" +
                    " FROM " +
                    " field";
            st = conn.createStatement();
            log.info("运行：{}", sql);
            rs = st.executeQuery(sql);
            log.info("总数：{}", rs.getRow());
            int i = 0;
            while (rs.next()) {
                String fieldName = rs.getString("FieldName");
                String oldId = rs.getString("ID");
                Integer number = rs.getInt("Number");

                Date createDate = new Date(rs.getTimestamp("create_date").getTime());
                Date updateDate = new Date(rs.getTimestamp("update_date").getTime());

                Field field = new Field();
                field.setName(fieldName);
                field.setOldId(oldId);
                field.setCapacity(number);
                field.setCreateTime(createDate);
                field.setUpdateTime(updateDate);

                i ++;
                log.info("招聘信息：{} 数量：{} 个", fieldName, i);
                if (fieldService.save(field)) {
                    log.info("插入招聘信息：{} 数量：{} 个", fieldName, i);
                }
            }
            long overTime = System.currentTimeMillis();
            long finishTime = (overTime - beginTime) / 1000;
            log.info("完成时间 : {} 分", finishTime);
            return JsonResult.ok(String.format("完成时间 : %s 分", finishTime));
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                st.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return JsonResult.ok();
    }

}
